package com.jiaxue.admin.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiaxue.admin.dao.ClientMapper;
import com.jiaxue.admin.dao.ErrCodeMapper;
import com.jiaxue.admin.dao.FlowOrderMapper;
import com.jiaxue.admin.dao.PaymentOrderMapper;
import com.jiaxue.admin.dao.StatisticsMapper;
import com.jiaxue.admin.model.PaymentOrder;
import com.jiaxue.admin.service.CoinService;

@Service("coinService")
public class CoinServiceImpl implements CoinService {
	
	@Autowired
	private ClientMapper clientMapper;
	@Autowired
	private PaymentOrderMapper paymentOrderMapper;
	@Autowired
	private FlowOrderMapper flowOrderMapper;
	@Autowired
	private StatisticsMapper statisticsMapper;
	@Autowired
	private ErrCodeMapper errCodeMapper;
	private static final Integer YIDONG=1;
	private static final Integer LIANTONG=2;
	private static final Integer DIANXIN=3;
	
	public List<Map<String,Integer>> getIphoneTypeSum() {
		return clientMapper.getIphoneTypeSum();
	}
	
	public Integer getCoinSum() {
		return paymentOrderMapper.getCoinSum();
	}

	public Integer getCoinConsume() {
		return flowOrderMapper.getCoinConsume();
	}

	public List iphoneSumList(Integer pageNum,Integer pageSize) {
		List<PaymentOrder> list=null;
		try {
		    //获取第1页，10条内容，默认查询总数count
		    PageHelper.startPage(pageNum, pageSize);
		    //紧跟着的第一个select方法会被分页
		    list = statisticsMapper.selectByPage();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public PageInfo selectClientSumByPage(String starttime,String  endtime,Integer pageNum,Integer pageSize) {
		 
		PageHelper.startPage(pageNum, pageSize);
		Map datemap=new HashedMap();
		datemap.put("starttime", starttime);
		datemap.put("endtime", endtime);
		List list = statisticsMapper.selectClientSumByPage(datemap);
		PageInfo page = new PageInfo(list);
		List<Map<String, String>> list2= page.getList();
		for(Map map :list2){
			String date = (String)map.get("date");
			Integer yidong = statisticsMapper.selectRegisterByIphoneTypeAndDate(YIDONG,date);
			Integer liantong = statisticsMapper.selectRegisterByIphoneTypeAndDate(LIANTONG,date);
			Integer dianxin = statisticsMapper.selectRegisterByIphoneTypeAndDate(DIANXIN,date);
			Integer AmountSum = statisticsMapper.selectAmountSumByDate(date);
			Integer AmountConsume = statisticsMapper.selectAmountConsumeByDate(date);
			Integer allRegister=Integer.parseInt(map.get("allRegister").toString());
			map.put("yidong", yidong);
			map.put("liantong", liantong);
			map.put("dianxin", dianxin);
			map.put("amountSum", AmountSum==null?0:AmountSum);
			map.put("amountConsume", AmountConsume==null?0:AmountConsume);
			System.out.println(map);
		}
		return page;
	}
	
	public PageInfo  selectStatisticsDetailByDateAndPhoneNo(String starttime,String  endtime,String phoneno,Integer pageNum,Integer pageSize){
		PageHelper.startPage(pageNum, pageSize);
		//1、查询用户详情
		List<Map> list = statisticsMapper.selectClientByDateAndPhoneNo(starttime, endtime, phoneno);
		for(Map map :list){
			Integer id = Integer.parseInt(map.get("id").toString());
			//Integer coinpay = Integer.parseInt(map.get("coinpay")==null?"0":map.get("coinpay").toString());
			//1、获取用户充值流量币
			Integer coinpay = statisticsMapper.selectPaySumById(id);
			coinpay=(coinpay==null?0:coinpay);
			//2、查询用户充值消耗的，无论给谁充值的
			Integer flowSum = statisticsMapper.selectFlowSumById(id);
			//3、用户转给别人的流量币
			Integer ownerToOther = statisticsMapper.selectOwnerToOtherById(id);
			//4、别人转给用户的流量币 
			Integer otherToOwner = statisticsMapper.selectOtherToOwnerById(id);
			//5、流量币总数  = 购买+他人转入 
			Integer coinSumInteger=coinpay+(otherToOwner==null?0:otherToOwner);
			//6、剩余流量币	=流量币总数-充值消耗流量币-转出流量币  
			Integer coinRemainInteger=coinSumInteger-(flowSum==null?0:flowSum)-(ownerToOther==null?0:ownerToOther);
			map.put("coinpay",coinpay);
			map.put("flowSum", flowSum==null?0:flowSum);
			map.put("ownerToOther", ownerToOther==null?0:ownerToOther);
			map.put("otherToOwner", otherToOwner==null?0:otherToOwner);
			map.put("coinSum", coinSumInteger);
			map.put("coinRemain", coinRemainInteger);
		}
		PageInfo page = new PageInfo(list);
		return page;
	}

	public PageInfo selectClientDetailById(Integer id,Integer pageNum,Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Map> list = statisticsMapper.selectClientDetailById(id);
		PageInfo page = new PageInfo(list);
		return page;
	}

	public String getPkgNoByOrderId(String orderId) {
		return flowOrderMapper.getPkgNoByOrderId(orderId);
	}

	public String getCodeDescByCode(String code) {
		return errCodeMapper.getCodeDescByCode(code);
	}
	
	
	
}
